package com.tool4j.controller;

import com.tool4j.entity.TemplateFile;
import com.tool4j.entity.TemplateInfo;
import com.tool4j.service.TemplateInfoService;
import com.tool4j.util.UserPermission;
import com.tool4j.util.http.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;

import java.util.List;

/**
 * 代码生成模板信息表
 *
 * @author Deng.Weiping
 * @since 2023-11-24 09:02:30
 */
@RestController
@RequestMapping("/api/templateInfo")
public class TemplateInfoController {

    @Autowired
    private TemplateInfoService templateInfoService;

    /**
     * 查询模板
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result<List<TemplateInfo>> findAll(ServerWebExchange exchange) {
        List<TemplateInfo> result = templateInfoService.findAll(UserPermission.get(exchange).getUserId());
        return Result.ok(result);
    }

    /**
     * 查询模板文件
     *
     * @return
     */
    @GetMapping("/getFiles/{id}")
    public Result<List<TemplateFile>> getFiles(@PathVariable("id") Long id) {
        List<TemplateFile> result = templateInfoService.getFiles(id);
        return Result.ok(result);
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<TemplateInfo> findById(@PathVariable("id") Long id) {
        TemplateInfo templateInfo = templateInfoService.findById(id);
        return Result.ok(templateInfo);
    }

    /**
     * 新增
     *
     * @param templateInfo
     * @return
     */
    @PostMapping
    public Result<Boolean> insert(ServerWebExchange exchange, @RequestBody TemplateInfo templateInfo) {
        boolean result = templateInfoService.insert(exchange, templateInfo);
        return Result.ok(result);
    }

    /**
     * 新增模板文件
     *
     * @param templateFile
     * @return
     */
    @PostMapping("/saveFile")
    public Result<Long> saveFile(@RequestBody TemplateFile templateFile) {
        Long id = templateInfoService.saveFile(templateFile);
        return Result.ok(id);
    }

    /**
     * 删除模板文件
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delFile/{id}")
    public Result<Long> delFile(@PathVariable("id") Long id) {
        templateInfoService.delFile(id);
        return Result.ok();
    }

    /**
     * 修改
     *
     * @param templateFile
     * @return
     */
    @PutMapping("/updateFile")
    public Result<Boolean> updateFile(ServerWebExchange exchange, @RequestBody TemplateFile templateFile) {
        boolean result = templateInfoService.updateFile(exchange, templateFile);
        return Result.ok(result);
    }

    /**
     * 重命名文件
     *
     * @param templateFile
     * @return
     */
    @PutMapping("/renameFile")
    public Result<Boolean> renameFile(ServerWebExchange exchange, @RequestBody TemplateFile templateFile) {
        boolean result = templateInfoService.renameFile(exchange, templateFile);
        return Result.ok(result);
    }

    /**
     * 重命名模板
     *
     * @param templateInfo
     * @return
     */
    @PutMapping("/rename")
    public Result<Boolean> rename(ServerWebExchange exchange, @RequestBody TemplateInfo templateInfo) {
        boolean result = templateInfoService.rename(exchange, templateInfo);
        return Result.ok(result);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Integer> delete(ServerWebExchange exchange, @PathVariable("id") Long id) {
        int result = templateInfoService.delete(exchange, id);
        return Result.ok(result);
    }

}